//
//  AppDelegate+YCPayDelegate.m
//  HeartWorldClient
//
//  Created by zhangji on 2016/11/29.
//  Copyright © 2016年 谭明权. All rights reserved.
//

#import "AppDelegate+YCPayDelegate.h"
#import <AlipaySDK/AlipaySDK.h>
#import "AlixPayResult.h"
#import "WXApiManager.h"
#import "MBProgressHUD.h"

@implementation AppDelegate (YCPayDelegate)

- (void)setUpPaySDKWithUrl:(NSURL *)url
{

    //跳转支付宝钱包进行支付，需要将支付宝钱包的支付结果回传给SDK
    if ([url.host isEqualToString:@"safepay"]) {
        [[AlipaySDK defaultService]
         processOrderWithPaymentResult:url
         standbyCallback:^(NSDictionary *resultDic) {
             NSLog(@"result = %@", resultDic);
             //结果处理
             AlixPayResult* result = [AlixPayResult itemWithDictory:resultDic];
             
             
             
             if (result)
             {
                 //                              状态返回9000为成功
                 if (result.statusCode == 9000)
                 {
                     /*
                      *用公钥验证签名 严格验证请使用result.resultString与result.signString验签
                      */
                     NSLog(@"支付宝交易成功");
                     
                     NSNotification *noti = [NSNotification notificationWithName:@"alipayStatus" object:@"9000"];
                     //    发送通知
                     [[NSNotificationCenter defaultCenter] postNotification:noti];
                     
                     
                     
                 }
                 else
                 {
                     //交易失败
                     NSLog(@"支付失败");
                     NSString *code = [NSString stringWithFormat:@"%d",result.statusCode];
                     NSNotification *noti = [NSNotification notificationWithName:@"alipayStatus" object:code];
                     //    发送通知
                     [[NSNotificationCenter defaultCenter] postNotification:noti];
                     
                 }
             }
             else
             {
                 //失败
                 NSLog(@"支付失败");
                 NSNotification *noti = [NSNotification notificationWithName:@"alipayStatus" object:nil];
                 //    发送通知
                 [[NSNotificationCenter defaultCenter] postNotification:noti];

             }
             
         }];
    }else{
        //微信
        
        /*
         */
        for (UIView  * hud in [UIApplication sharedApplication].keyWindow.subviews)
        {
            if ([hud isKindOfClass:[MBProgressHUD class]])
            {
                [[UIApplication sharedApplication].keyWindow dismissTips];
            }
        }
        
        [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
        
    }



}

@end
